package com.customer.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
/**
 * WebSocket配置类
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    
    @Autowired
    private StreamingChatHandler streamingChatHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册流式对话的WebSocket处理器
        registry.addHandler(streamingChatHandler, "/ws/chat")
                .setAllowedOrigins("*"); // 允许跨域访问
    }

    // 配置WebSocket服务器容器，增加消息大小限制
    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        // 增加文本消息缓冲区大小到5MB，以处理更大的音频数据
        container.setMaxTextMessageBufferSize(1024 * 1024 * 5);
        // 增加二进制消息缓冲区大小到5MB
        container.setMaxBinaryMessageBufferSize(1024 * 1024 * 5);
        // 设置最大会话空闲超时时间（毫秒）
        container.setMaxSessionIdleTimeout(30 * 60 * 1000L); // 30分钟
        return container;
    }
}